/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lvyh.lightframe.cloud.business.usercenter.service;

import com.github.pagehelper.Page;
import com.lvyh.lightframe.cloud.business.usercenter.bean.UcSysMenuBean;
import com.lvyh.lightframe.cloud.business.usercenter.bean.UcSysRoleBean;
import com.lvyh.lightframe.cloud.business.usercenter.bean.UcSysRoleMenuBean;
import com.lvyh.lightframe.cloud.business.usercenter.bean.UcSysRoleUserBean;
import com.lvyh.lightframe.cloud.business.usercenter.bean.UcSysUserBean;
import com.lvyh.lightframe.cloud.business.usercenter.dao.entity.UcSysMenu;
import com.lvyh.lightframe.cloud.business.usercenter.dao.entity.UcSysMenuExample;
import com.lvyh.lightframe.cloud.business.usercenter.dao.entity.UcSysRole;
import com.lvyh.lightframe.cloud.business.usercenter.dao.entity.UcSysRoleExample;
import com.lvyh.lightframe.cloud.business.usercenter.dao.entity.UcSysRoleMenu;
import com.lvyh.lightframe.cloud.business.usercenter.dao.entity.UcSysRoleMenuExample;
import com.lvyh.lightframe.cloud.business.usercenter.dao.entity.UcSysRoleUser;
import com.lvyh.lightframe.cloud.business.usercenter.dao.entity.UcSysRoleUserExample;
import com.lvyh.lightframe.cloud.business.usercenter.dao.entity.UcSysUser;
import com.lvyh.lightframe.cloud.business.usercenter.dao.entity.UcSysUserExample;
import com.lvyh.lightframe.cloud.business.usercenter.dao.mapper.UcSysMenuMapper;
import com.lvyh.lightframe.cloud.business.usercenter.dao.mapper.UcSysRoleMapper;
import com.lvyh.lightframe.cloud.business.usercenter.dao.mapper.UcSysRoleMenuMapper;
import com.lvyh.lightframe.cloud.business.usercenter.dao.mapper.UcSysRoleUserMapper;
import com.lvyh.lightframe.cloud.business.usercenter.dao.mapper.UcSysUserMapper;
import com.lvyh.lightframe.cloud.common.pagination.DefaultPageResult;
import com.lvyh.lightframe.cloud.common.pagination.PageParam;
import com.lvyh.lightframe.cloud.common.pagination.PageUtil;
import com.lvyh.lightframe.cloud.common.util.BeanUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author lvyh 2021/04/06.
 */
@Service
public class UserCenterMainDataService {
    private final static Logger logger = LoggerFactory.getLogger(UserCenterMainDataService.class);

    @Autowired
    private UcSysMenuMapper ucSysMenuMapper;

    @Autowired
    private UcSysRoleMapper ucSysRoleMapper;

    @Autowired
    private UcSysRoleMenuMapper ucSysRoleMenuMapper;

    @Autowired
    private UcSysRoleUserMapper ucSysRoleUserMapper;

    @Autowired
    private UcSysUserMapper ucSysUserMapper;


    public boolean deleteUcSysUser(Integer id) throws Exception {
        if (id == null) {
            return false;
        }
        try {
            int updateNum = ucSysUserMapper.deleteByPrimaryKey(id);
            return updateNum == 1;
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }

    public List<UcSysUserBean> selectUcSysUser(UcSysUserBean condition) throws Exception {
        List<UcSysUserBean> list = new ArrayList<UcSysUserBean>();
        try {
            UcSysUserExample example = new UcSysUserExample();
            UcSysUserExample.Criteria criteria = example.createCriteria();
            if (condition.getId() != null) {
                criteria.andIdEqualTo(condition.getId());
            }
            if (!StringUtils.isEmpty(condition.getMobile())) {
                criteria.andMobileEqualTo(condition.getMobile());
            }
            if (!StringUtils.isEmpty(condition.getUserName())) {
                criteria.andUserNameEqualTo(condition.getUserName());
            }
            if (!StringUtils.isEmpty(condition.getOpenId())) {
                criteria.andOpenIdEqualTo(condition.getOpenId());
            }

            List<UcSysUser> queryDataItems = ucSysUserMapper.selectByExample(example);
            if (CollectionUtils.isEmpty(queryDataItems)) {
                return list;
            }

            for (UcSysUser ucSysUser : queryDataItems) {
                list.add(BeanUtil.toBean(UcSysUserBean.class, BeanUtil.toMap(ucSysUser)));
            }

        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
        return list;
    }

    public DefaultPageResult<UcSysUserBean> selectUcSysUserByCondition(UcSysUserBean condition, PageParam pageParam) throws Exception {
        if (pageParam == null) {
            throw new Exception("pageParam must need");
        }
        Page<UcSysUser> page = PageUtil.startPage(pageParam);

        try {
            UcSysUserExample example = new UcSysUserExample();
            if (condition != null) {
                UcSysUserExample.Criteria criteria = example.createCriteria();
                if (condition.getId() != null) {
                    criteria.andIdEqualTo(condition.getId());
                }

            }

            List<UcSysUser> queryDataItems = ucSysUserMapper.selectByExample(example);
            DefaultPageResult<UcSysUserBean> result = new DefaultPageResult<>();
            if (queryDataItems == null || queryDataItems.size() == 0) {
                return result;
            }

            List<UcSysUserBean> list = new ArrayList<UcSysUserBean>();
            for (UcSysUser ucSysUser : queryDataItems) {
                list.add(BeanUtil.toBean(UcSysUserBean.class, BeanUtil.toMap(ucSysUser)));
            }

            result.setResultList(list);
            result.setPageSize(page.getPageSize());
            result.setPageNo(page.getPageNum());
            result.setTotalCount((int) page.getTotal());
            result.setTotalPage(page.getPages());
            return result;
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }

    public int countUcSysUserByCondition(UcSysUserBean condition) throws Exception {
        try {
            UcSysUserExample example = new UcSysUserExample();
            if (condition != null) {
                UcSysUserExample.Criteria criteria = example.createCriteria();
                if (condition.getId() != null) {
                    criteria.andIdEqualTo(condition.getId());
                }


            }

            return ucSysUserMapper.countByExample(example);
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }

    @Transactional(rollbackFor = {RuntimeException.class})
    public boolean updateUcSysUser(UcSysUserBean ucSysUserBean) throws Exception {
        if (ucSysUserBean == null || ucSysUserBean.getId() <= 0) {
            return false;
        }
        try {
            ucSysUserBean.setUpdateTime(new Date());
            UcSysUser updateBean = BeanUtil.toBean(UcSysUser.class, BeanUtil.toMap(ucSysUserBean));
            int updateNum = ucSysUserMapper.updateByPrimaryKeySelective(updateBean);
            return updateNum == 1;
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }

    public UcSysUserBean selectUcSysUserById(Integer id) throws Exception {
        if (id <= 0) {
            return null;
        }
        try {
            UcSysUser ucSysUser = ucSysUserMapper.selectByPrimaryKey(id);
            if (ucSysUser == null) {
                logger.warn("not found data, tid:{}", id);
                return null;
            }
            return BeanUtil.toBean(UcSysUserBean.class, BeanUtil.toMap(ucSysUser));
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }

    public Integer saveUcSysUser(UcSysUserBean ucSysUserBean) throws Exception {
        if (ucSysUserBean == null) {
            return -1;
        }
        try {
            ucSysUserBean.setCreateTime(new Date());
            ucSysUserBean.setUpdateTime(new Date());
            UcSysUser insertBean = BeanUtil.toBean(UcSysUser.class, BeanUtil.toMap(ucSysUserBean));
            ucSysUserMapper.insertSelective(insertBean);
            return insertBean.getId();
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }

    public boolean deleteUcSysRoleUser(Integer id) throws Exception {
        if (id == null) {
            return false;
        }
        try {
            int updateNum = ucSysRoleUserMapper.deleteByPrimaryKey(id);
            return updateNum == 1;
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }

    public List<UcSysRoleUserBean> selectUcSysRoleUser(UcSysRoleUserBean condition) throws Exception {
        List<UcSysRoleUserBean> list = new ArrayList<UcSysRoleUserBean>();
        try {
            UcSysRoleUserExample example = new UcSysRoleUserExample();
            UcSysRoleUserExample.Criteria criteria = example.createCriteria();
            if (condition.getId() != null) {
                criteria.andIdEqualTo(condition.getId());
            }
            if (condition.getRoleId() != null) {
                criteria.andRoleIdEqualTo(condition.getRoleId());
            }

            if (condition.getUserId() != null) {
                criteria.andUserIdEqualTo(condition.getUserId());
            }

            List<UcSysRoleUser> queryDataItems = ucSysRoleUserMapper.selectByExample(example);
            if (CollectionUtils.isEmpty(queryDataItems)) {
                return list;
            }

            for (UcSysRoleUser ucSysRoleUser : queryDataItems) {
                list.add(BeanUtil.toBean(UcSysRoleUserBean.class, BeanUtil.toMap(ucSysRoleUser)));
            }

        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
        return list;
    }

    public DefaultPageResult<UcSysRoleUserBean> selectUcSysRoleUserByCondition(UcSysRoleUserBean condition, PageParam pageParam) throws Exception {
        if (pageParam == null) {
            throw new Exception("pageParam must need");
        }
        Page<UcSysRoleUser> page = PageUtil.startPage(pageParam);

        try {
            UcSysRoleUserExample example = new UcSysRoleUserExample();
            if (condition != null) {
                UcSysRoleUserExample.Criteria criteria = example.createCriteria();
                if (condition.getId() != null) {
                    criteria.andIdEqualTo(condition.getId());
                }

            }

            List<UcSysRoleUser> queryDataItems = ucSysRoleUserMapper.selectByExample(example);
            DefaultPageResult<UcSysRoleUserBean> result = new DefaultPageResult<>();
            if (queryDataItems == null || queryDataItems.size() == 0) {
                return result;
            }

            List<UcSysRoleUserBean> list = new ArrayList<UcSysRoleUserBean>();
            for (UcSysRoleUser ucSysRoleUser : queryDataItems) {
                list.add(BeanUtil.toBean(UcSysRoleUserBean.class, BeanUtil.toMap(ucSysRoleUser)));
            }

            result.setResultList(list);
            result.setPageSize(page.getPageSize());
            result.setPageNo(page.getPageNum());
            result.setTotalCount((int) page.getTotal());
            result.setTotalPage(page.getPages());
            return result;
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }

    public int countUcSysRoleUserByCondition(UcSysRoleUserBean condition) throws Exception {
        try {
            UcSysRoleUserExample example = new UcSysRoleUserExample();
            if (condition != null) {
                UcSysRoleUserExample.Criteria criteria = example.createCriteria();
                if (condition.getId() != null) {
                    criteria.andIdEqualTo(condition.getId());
                }


            }

            return ucSysRoleUserMapper.countByExample(example);
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }

    @Transactional(rollbackFor = {RuntimeException.class})
    public boolean updateUcSysRoleUser(UcSysRoleUserBean ucSysRoleUserBean) throws Exception {
        if (ucSysRoleUserBean == null || ucSysRoleUserBean.getId() <= 0) {
            return false;
        }
        try {
            UcSysRoleUser updateBean = BeanUtil.toBean(UcSysRoleUser.class, BeanUtil.toMap(ucSysRoleUserBean));
            int updateNum = ucSysRoleUserMapper.updateByPrimaryKeySelective(updateBean);
            return updateNum == 1;
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }

    public UcSysRoleUserBean selectUcSysRoleUserById(Integer id) throws Exception {
        if (id <= 0) {
            return null;
        }
        try {
            UcSysRoleUser ucSysRoleUser = ucSysRoleUserMapper.selectByPrimaryKey(id);
            if (ucSysRoleUser == null) {
                logger.warn("not found data, tid:{}", id);
                return null;
            }
            return BeanUtil.toBean(UcSysRoleUserBean.class, BeanUtil.toMap(ucSysRoleUser));
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }

    public Integer saveUcSysRoleUser(UcSysRoleUserBean ucSysRoleUserBean) throws Exception {
        if (ucSysRoleUserBean == null) {
            return -1;
        }
        try {
            UcSysRoleUser insertBean = BeanUtil.toBean(UcSysRoleUser.class, BeanUtil.toMap(ucSysRoleUserBean));
            ucSysRoleUserMapper.insertSelective(insertBean);
            return insertBean.getId();
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }

    public boolean deleteUcSysRoleMenu(Integer id) throws Exception {
        if (id == null) {
            return false;
        }
        try {
            int updateNum = ucSysRoleMenuMapper.deleteByPrimaryKey(id);
            return updateNum == 1;
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }

    public List<UcSysRoleMenuBean> selectUcSysRoleMenu(UcSysRoleMenuBean condition) throws Exception {
        List<UcSysRoleMenuBean> list = new ArrayList<UcSysRoleMenuBean>();
        try {
            UcSysRoleMenuExample example = new UcSysRoleMenuExample();
            UcSysRoleMenuExample.Criteria criteria = example.createCriteria();
            if (condition.getId() != null) {
                criteria.andIdEqualTo(condition.getId());
            }
            if (condition.getRoleId() != null) {
                criteria.andRoleIdEqualTo(condition.getRoleId());
            }

            if (condition.getMenuId() != null) {
                criteria.andMenuIdEqualTo(condition.getMenuId());
            }
            if (!CollectionUtils.isEmpty(condition.getRoleIds())) {
                criteria.andRoleIdIn(condition.getRoleIds());
            }

            if (!CollectionUtils.isEmpty(condition.getMenuIds())) {
                criteria.andMenuIdIn(condition.getMenuIds());
            }

            List<UcSysRoleMenu> queryDataItems = ucSysRoleMenuMapper.selectByExample(example);
            if (CollectionUtils.isEmpty(queryDataItems)) {
                return list;
            }

            for (UcSysRoleMenu ucSysRoleMenu : queryDataItems) {
                list.add(BeanUtil.toBean(UcSysRoleMenuBean.class, BeanUtil.toMap(ucSysRoleMenu)));
            }

        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
        return list;
    }

    public DefaultPageResult<UcSysRoleMenuBean> selectUcSysRoleMenuByCondition(UcSysRoleMenuBean condition, PageParam pageParam) throws Exception {
        if (pageParam == null) {
            throw new Exception("pageParam must need");
        }
        Page<UcSysRoleMenu> page = PageUtil.startPage(pageParam);

        try {
            UcSysRoleMenuExample example = new UcSysRoleMenuExample();
            if (condition != null) {
                UcSysRoleMenuExample.Criteria criteria = example.createCriteria();
                if (condition.getId() != null) {
                    criteria.andIdEqualTo(condition.getId());
                }

            }

            List<UcSysRoleMenu> queryDataItems = ucSysRoleMenuMapper.selectByExample(example);
            DefaultPageResult<UcSysRoleMenuBean> result = new DefaultPageResult<>();
            if (queryDataItems == null || queryDataItems.size() == 0) {
                return result;
            }

            List<UcSysRoleMenuBean> list = new ArrayList<UcSysRoleMenuBean>();
            for (UcSysRoleMenu ucSysRoleMenu : queryDataItems) {
                list.add(BeanUtil.toBean(UcSysRoleMenuBean.class, BeanUtil.toMap(ucSysRoleMenu)));
            }

            result.setResultList(list);
            result.setPageSize(page.getPageSize());
            result.setPageNo(page.getPageNum());
            result.setTotalCount((int) page.getTotal());
            result.setTotalPage(page.getPages());
            return result;
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }

    public int countUcSysRoleMenuByCondition(UcSysRoleMenuBean condition) throws Exception {
        try {
            UcSysRoleMenuExample example = new UcSysRoleMenuExample();
            if (condition != null) {
                UcSysRoleMenuExample.Criteria criteria = example.createCriteria();
                if (condition.getId() != null) {
                    criteria.andIdEqualTo(condition.getId());
                }


            }

            return ucSysRoleMenuMapper.countByExample(example);
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }

    @Transactional(rollbackFor = {RuntimeException.class})
    public boolean updateUcSysRoleMenu(UcSysRoleMenuBean ucSysRoleMenuBean) throws Exception {
        if (ucSysRoleMenuBean == null || ucSysRoleMenuBean.getId() <= 0) {
            return false;
        }
        try {
            UcSysRoleMenu updateBean = BeanUtil.toBean(UcSysRoleMenu.class, BeanUtil.toMap(ucSysRoleMenuBean));
            int updateNum = ucSysRoleMenuMapper.updateByPrimaryKeySelective(updateBean);
            return updateNum == 1;
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }

    public UcSysRoleMenuBean selectUcSysRoleMenuById(Integer id) throws Exception {
        if (id <= 0) {
            return null;
        }
        try {
            UcSysRoleMenu ucSysRoleMenu = ucSysRoleMenuMapper.selectByPrimaryKey(id);
            if (ucSysRoleMenu == null) {
                logger.warn("not found data, tid:{}", id);
                return null;
            }
            return BeanUtil.toBean(UcSysRoleMenuBean.class, BeanUtil.toMap(ucSysRoleMenu));
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }

    public Integer saveUcSysRoleMenu(UcSysRoleMenuBean ucSysRoleMenuBean) throws Exception {
        if (ucSysRoleMenuBean == null) {
            return -1;
        }
        try {
            UcSysRoleMenu insertBean = BeanUtil.toBean(UcSysRoleMenu.class, BeanUtil.toMap(ucSysRoleMenuBean));
            ucSysRoleMenuMapper.insertSelective(insertBean);
            return insertBean.getId();
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }

    public boolean deleteUcSysRole(Integer id) throws Exception {
        if (id == null) {
            return false;
        }
        try {
            int updateNum = ucSysRoleMapper.deleteByPrimaryKey(id);
            return updateNum == 1;
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }

    public List<UcSysRoleBean> selectUcSysRole(UcSysRoleBean condition) throws Exception {
        List<UcSysRoleBean> list = new ArrayList<UcSysRoleBean>();
        try {
            UcSysRoleExample example = new UcSysRoleExample();
            UcSysRoleExample.Criteria criteria = example.createCriteria();
            if (condition.getId() != null) {
                criteria.andIdEqualTo(condition.getId());
            }

            if (!StringUtils.isEmpty(condition.getCode())) {
                criteria.andCodeEqualTo(condition.getCode());
            }

            if (!CollectionUtils.isEmpty(condition.getIds())) {
                criteria.andIdIn(condition.getIds());
            }

            if (!CollectionUtils.isEmpty(condition.getCodes())) {
                criteria.andCodeIn(condition.getCodes());
            }

            List<UcSysRole> queryDataItems = ucSysRoleMapper.selectByExample(example);
            if (CollectionUtils.isEmpty(queryDataItems)) {
                return list;
            }

            for (UcSysRole ucSysRole : queryDataItems) {
                list.add(BeanUtil.toBean(UcSysRoleBean.class, BeanUtil.toMap(ucSysRole)));
            }

        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
        return list;
    }

    public DefaultPageResult<UcSysRoleBean> selectUcSysRoleByCondition(UcSysRoleBean condition, PageParam pageParam) throws Exception {
        if (pageParam == null) {
            throw new Exception("pageParam must need");
        }
        Page<UcSysRole> page = PageUtil.startPage(pageParam);

        try {
            UcSysRoleExample example = new UcSysRoleExample();
            if (condition != null) {
                UcSysRoleExample.Criteria criteria = example.createCriteria();
                if (condition.getId() != null) {
                    criteria.andIdEqualTo(condition.getId());
                }

            }

            List<UcSysRole> queryDataItems = ucSysRoleMapper.selectByExample(example);
            DefaultPageResult<UcSysRoleBean> result = new DefaultPageResult<>();
            if (queryDataItems == null || queryDataItems.size() == 0) {
                return result;
            }

            List<UcSysRoleBean> list = new ArrayList<UcSysRoleBean>();
            for (UcSysRole ucSysRole : queryDataItems) {
                list.add(BeanUtil.toBean(UcSysRoleBean.class, BeanUtil.toMap(ucSysRole)));
            }

            result.setResultList(list);
            result.setPageSize(page.getPageSize());
            result.setPageNo(page.getPageNum());
            result.setTotalCount((int) page.getTotal());
            result.setTotalPage(page.getPages());
            return result;
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }

    public int countUcSysRoleByCondition(UcSysRoleBean condition) throws Exception {
        try {
            UcSysRoleExample example = new UcSysRoleExample();
            if (condition != null) {
                UcSysRoleExample.Criteria criteria = example.createCriteria();
                if (condition.getId() != null) {
                    criteria.andIdEqualTo(condition.getId());
                }


            }

            return ucSysRoleMapper.countByExample(example);
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }

    @Transactional(rollbackFor = {RuntimeException.class})
    public boolean updateUcSysRole(UcSysRoleBean ucSysRoleBean) throws Exception {
        if (ucSysRoleBean == null || ucSysRoleBean.getId() <= 0) {
            return false;
        }
        try {
            ucSysRoleBean.setUpdateTime(new Date());
            UcSysRole updateBean = BeanUtil.toBean(UcSysRole.class, BeanUtil.toMap(ucSysRoleBean));
            int updateNum = ucSysRoleMapper.updateByPrimaryKeySelective(updateBean);
            return updateNum == 1;
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }

    public UcSysRoleBean selectUcSysRoleById(Integer id) throws Exception {
        if (id <= 0) {
            return null;
        }
        try {
            UcSysRole ucSysRole = ucSysRoleMapper.selectByPrimaryKey(id);
            if (ucSysRole == null) {
                logger.warn("not found data, tid:{}", id);
                return null;
            }
            return BeanUtil.toBean(UcSysRoleBean.class, BeanUtil.toMap(ucSysRole));
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }

    public Integer saveUcSysRole(UcSysRoleBean ucSysRoleBean) throws Exception {
        if (ucSysRoleBean == null) {
            return -1;
        }
        try {
            ucSysRoleBean.setCreateTime(new Date());
            ucSysRoleBean.setUpdateTime(new Date());
            UcSysRole insertBean = BeanUtil.toBean(UcSysRole.class, BeanUtil.toMap(ucSysRoleBean));
            ucSysRoleMapper.insertSelective(insertBean);
            return insertBean.getId();
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }

    public boolean deleteUcSysMenu(Integer id) throws Exception {
        if (id == null) {
            return false;
        }
        try {
            int updateNum = ucSysMenuMapper.deleteByPrimaryKey(id);
            return updateNum == 1;
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }

    public List<UcSysMenuBean> selectUcSysMenu(UcSysMenuBean condition) throws Exception {
        List<UcSysMenuBean> list = new ArrayList<UcSysMenuBean>();
        try {
            UcSysMenuExample example = new UcSysMenuExample();
            UcSysMenuExample.Criteria criteria = example.createCriteria();
            if (condition.getId() != null) {
                criteria.andIdEqualTo(condition.getId());
            }
            if (condition.getType() != null) {
                criteria.andTypeEqualTo(condition.getType());
            }
            if (condition.getHidden() != null) {
                criteria.andHiddenEqualTo(condition.getHidden());
            }
            if (!CollectionUtils.isEmpty(condition.getIds())) {
                criteria.andIdIn(condition.getIds());
            }

            List<UcSysMenu> queryDataItems = ucSysMenuMapper.selectByExample(example);
            if (CollectionUtils.isEmpty(queryDataItems)) {
                return list;
            }

            for (UcSysMenu ucSysMenu : queryDataItems) {
                list.add(BeanUtil.toBean(UcSysMenuBean.class, BeanUtil.toMap(ucSysMenu)));
            }

        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
        return list;
    }

    public DefaultPageResult<UcSysMenuBean> selectUcSysMenuByCondition(UcSysMenuBean condition, PageParam pageParam) throws Exception {
        if (pageParam == null) {
            throw new Exception("pageParam must need");
        }
        Page<UcSysMenu> page = PageUtil.startPage(pageParam);

        try {
            UcSysMenuExample example = new UcSysMenuExample();
            if (condition != null) {
                UcSysMenuExample.Criteria criteria = example.createCriteria();
                if (condition.getId() != null) {
                    criteria.andIdEqualTo(condition.getId());
                }

            }

            List<UcSysMenu> queryDataItems = ucSysMenuMapper.selectByExample(example);
            DefaultPageResult<UcSysMenuBean> result = new DefaultPageResult<>();
            if (queryDataItems == null || queryDataItems.size() == 0) {
                return result;
            }

            List<UcSysMenuBean> list = new ArrayList<UcSysMenuBean>();
            for (UcSysMenu ucSysMenu : queryDataItems) {
                list.add(BeanUtil.toBean(UcSysMenuBean.class, BeanUtil.toMap(ucSysMenu)));
            }

            result.setResultList(list);
            result.setPageSize(page.getPageSize());
            result.setPageNo(page.getPageNum());
            result.setTotalCount((int) page.getTotal());
            result.setTotalPage(page.getPages());
            return result;
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }

    public int countUcSysMenuByCondition(UcSysMenuBean condition) throws Exception {
        try {
            UcSysMenuExample example = new UcSysMenuExample();
            if (condition != null) {
                UcSysMenuExample.Criteria criteria = example.createCriteria();
                if (condition.getId() != null) {
                    criteria.andIdEqualTo(condition.getId());
                }


            }

            return ucSysMenuMapper.countByExample(example);
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }

    @Transactional(rollbackFor = {RuntimeException.class})
    public boolean updateUcSysMenu(UcSysMenuBean ucSysMenuBean) throws Exception {
        if (ucSysMenuBean == null || ucSysMenuBean.getId() <= 0) {
            return false;
        }
        try {
            ucSysMenuBean.setUpdateTime(new Date());
            UcSysMenu updateBean = BeanUtil.toBean(UcSysMenu.class, BeanUtil.toMap(ucSysMenuBean));
            int updateNum = ucSysMenuMapper.updateByPrimaryKeySelective(updateBean);
            return updateNum == 1;
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }

    public UcSysMenuBean selectUcSysMenuById(Integer id) throws Exception {
        if (id <= 0) {
            return null;
        }
        try {
            UcSysMenu ucSysMenu = ucSysMenuMapper.selectByPrimaryKey(id);
            if (ucSysMenu == null) {
                logger.warn("not found data, tid:{}", id);
                return null;
            }
            return BeanUtil.toBean(UcSysMenuBean.class, BeanUtil.toMap(ucSysMenu));
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }

    public Integer saveUcSysMenu(UcSysMenuBean ucSysMenuBean) throws Exception {
        if (ucSysMenuBean == null) {
            return -1;
        }
        try {
            ucSysMenuBean.setCreateTime(new Date());
            ucSysMenuBean.setUpdateTime(new Date());
            UcSysMenu insertBean = BeanUtil.toBean(UcSysMenu.class, BeanUtil.toMap(ucSysMenuBean));
            ucSysMenuMapper.insertSelective(insertBean);
            return insertBean.getId();
        } catch (Exception exp) {
            logger.error(exp.getMessage(), exp);
            throw exp;
        }
    }
}
